﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmReportTaskHistory : BaseForm
    {
        #region Constants
        private const String REPORT_TASK_HISTORY_GENERAL = "Relatório Histórico de Interrupção - Geral";
        private const String REPORT_TASK_HISTORY_ARCHITECTS = "Relatório Histórico de Interrupção - Geral - Por Arquiteto";
        private const String REPORT_TASK_HISTORY_PROJECTS = "Relatório Histórico de Interrupção - Geral - Por Projeto";
        private const String REPORT_TASK_HISTORY_CLASSIFICATION = "Relatório Histórico de Interrupção - Geral - Por Classificação";
        #endregion

        #region Starting Form
        public FrmReportTaskHistory()
        {
            InitializeComponent();
        }
        private void FrmReportTaskHistory_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            try
            {
                CleanFilter();
            }
            catch (Exception ex)
            {
                HandleException("Inicialização da tela", ex);
            }
        }
        #endregion

        #region Events
        private void cmdGenerateReport_Click(object sender, EventArgs e)
        {
            GenerateReport();
        }
        private void cmdCleanFilter_Click(object sender, EventArgs e)
        {
            CleanFilter();
        }
        #endregion

        #region Methods
        public void CleanFilter()
        {
            ucTaskHistoryReportFilter1.CleanFilter();
            opGroupGeneral.Checked = true;
        }
        private void GenerateReport()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                BusinessEntity.TaskHistoryFilter filter = ucTaskHistoryReportFilter1.GetReportFilter();
                String filterDescription = GetFilterDescription();
                List<BusinessEntity.TaskHistoryReport> historyList = null;
                FrmReportViewer wd = null;

                historyList = new BusinessLogic.GTA().ListTaskHistoryReport(filter);
                if (opGroupGeneral.Checked) // General Visualization - Without groupping
                {
                    Files.Reports.ReportTaskHistoryGeneral reportMG = new GTA.Files.Reports.ReportTaskHistoryGeneral();
                    reportMG.SetDataSource(historyList);
                    wd = new FrmReportViewer(reportMG);
                    wd.Text = REPORT_TASK_HISTORY_GENERAL;
                    reportMG.SetParameterValue("param_filter", filterDescription);
                }
                else if (opGroupArchitect.Checked) // Groupping by Architects
                {
                    Files.Reports.ReportTaskHistoryGeneralArchitect reportMA = new GTA.Files.Reports.ReportTaskHistoryGeneralArchitect();
                    reportMA.SetDataSource(historyList);
                    wd = new FrmReportViewer(reportMA);
                    wd.Text = REPORT_TASK_HISTORY_ARCHITECTS;
                    reportMA.SetParameterValue("param_filter", filterDescription);
                }
                else if (opGroupProject.Checked) // Groupping by Projects
                {
                    Files.Reports.ReportTaskHistoryGeneralProject reportMP = new GTA.Files.Reports.ReportTaskHistoryGeneralProject();
                    reportMP.SetDataSource(historyList);
                    wd = new FrmReportViewer(reportMP);
                    wd.Text = REPORT_TASK_HISTORY_PROJECTS;
                    reportMP.SetParameterValue("param_filter", filterDescription);
                }
                else if (opGroupClassification.Checked) // Groupping by Classification
                {
                    Files.Reports.ReportTaskHistoryGeneralClassification reportMC = new GTA.Files.Reports.ReportTaskHistoryGeneralClassification();
                    reportMC.SetDataSource(historyList);
                    wd = new FrmReportViewer(reportMC);
                    wd.Text = REPORT_TASK_HISTORY_CLASSIFICATION;
                    reportMC.SetParameterValue("param_filter", filterDescription);
                }

                if (wd != null)
                {
                    wd.MdiParent = this.MdiParent;
                    wd.Show();
                }
            }
            catch (Exception ex)
            {
                HandleException("Gerar o relatório", ex);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
        private string GetFilterDescription()
        {
            String filterDescription = String.Empty;

            filterDescription += ucTaskHistoryReportFilter1.GetFilterDescription();

            if (String.IsNullOrEmpty(filterDescription))
                filterDescription = "Nenhum";

            return filterDescription;
        }
        #endregion
    }
}
